package com.zhangliang.oa.crud;

import com.zhangliang.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet({"/dept/list","/dept/select","/dept/edit","/dept/update","/dept/add","/dept/delete"})
public class DeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        if ("dept/list".equals(servletPath)) {
            doList(req,resp);
        }else if ("/dept/select".equals(servletPath)){
            doSelect(req,resp);
        }else if ("/dept/edit".equals(servletPath)){
            doEdit(req,resp);
        }else if ("/dept/update".equals(servletPath)){
            doUpdate(req,resp);
        }else if ("/dept/add".equals(servletPath)){
            doAdd(req,resp);
        }else if ("/dept/delete".equals(servletPath)){
            doDel(req,resp);
        }
    }

    private void doDel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html");
        String contextPath = req.getContextPath();
        String deptno = req.getParameter("deptno");
        Connection conn = null;
        PreparedStatement ps = null;
        int i = 0;
        try {
            conn = DBUtil.getConnection();
            String sql ="delete from dept where deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,ps,null);
        if (i == 1){
            resp.sendRedirect(contextPath+"/dept/list");
        }else {
            resp.sendRedirect(contextPath+"/error.html");
        }
    }

    private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String contextPath = req.getContextPath();
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        Connection conn = null;
        PreparedStatement ps = null;
        String deptno = req.getParameter("deptno");
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc");
        int i = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "insert into dept (deptno,dname,loc) values (?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            ps.setString(2,dname);
            ps.setString(3,loc);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,ps,null);
        if (i == 1){
            resp.sendRedirect(contextPath+"/dept/list");
        } else {
            resp.sendRedirect(contextPath+"/error.html");
        }
    }

    private void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String contextPath = req.getContextPath();
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        String deptno = req.getParameter("deptno");
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc");
        Connection conn = null;
        PreparedStatement ps = null;
        int i = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "update dept set dname=?,loc=? where deptno=?";
            ps = conn.prepareStatement(sql);
            ps.setString(3,deptno);
            ps.setString(1,dname);
            ps.setString(2,loc);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,ps,null);
        if (i == 1){
            resp.sendRedirect(contextPath+"/dept/list");
        } else {
            resp.sendRedirect(contextPath+"/error.html");
        }
    }

    private void doEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String contextPath = req.getContextPath();
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<html lang='en'>");
        out.print("<head>");
        out.print("    <meta charset='UTF-8'>");
        out.print("    <title>select</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<form action='"+contextPath+"/dept/update' method='post'>");
        String deptno =req.getParameter("deptno");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select dname,loc from dept where deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            rs = ps.executeQuery();
            while (rs.next()){
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                out.print("<input type='text' name = 'deptno' value="+deptno+" readonly/><br>");
                out.print("<input type='text' name = 'dname' value='"+dname+"'/><br>");
                out.print("<input type='text' name = 'loc' value='"+loc+"'/><br>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,ps,rs);
        out.print("<input type='submit' value='修改'>");
        out.print("</form>");
        out.print("</body>");
        out.print("</html>");
    }

    private void doSelect(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<html lang='en'>");
        out.print("<head>");
        out.print("    <meta charset='UTF-8'>");
        out.print("    <title>select</title>");
        out.print("</head>");
        out.print("<body>");
        String deptno =req.getParameter("deptno");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select dname,loc from dept where deptno = ?;";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            rs = ps.executeQuery();
            while (rs.next()){
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                out.print("        部门编号："+deptno+"<br>");
                out.print("        部门名称："+dname+"<br>");
                out.print("        工作地点："+loc+"<br>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,ps,rs);
        out.print("<input type='button'' value='返回' onclick='window.history.back()'>");
        out.print("</body>");
        out.print("</html>");
    }

    private void doList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String contextPath = req.getContextPath();
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<html lang='en'>");
        out.print("<head>");
        out.print("    <meta charset='UTF-8'>");
        out.print("    <title>list</title>");
        out.print("<script type='text/javascript'>");
        out.print("            function del(dno){");
        out.print("        if(window.confirm('确认删除?')){");
        out.print("            document.location.href='"+contextPath+"/dept/delete?deptno='+dno;");
        out.print("        }");
        out.print("    }");
        out.print("</script>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h1 align='center'>部门列表</h1>");
        out.print("<hr>");
        out.print("<table border='1px' width='50%' align='center'>");
        out.print("    <tr>");
        out.print("        <th>部门编号</th>");
        out.print("        <th>部门名称</th>");
        out.print("        <th>工作地点</th>");
        out.print("        <th>操作</th>");
        out.print("    </tr>");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select deptno,dname,loc from dept;";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                String deptno = rs.getString("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                out.print("    <tr align='center'>");
                out.print("        <td>"+deptno+"</td>");
                out.print("        <td>"+dname+"</td>");
                out.print("        <td>"+loc+"</td>");
                out.print("        <td>");
                out.print("            <a href='javascript:(0)' onclick='del("+deptno+")'>删除</a>");
                out.print("            <a href='"+contextPath+"/dept/edit?deptno="+deptno+"'>修改</a>");
                out.print("            <a href='"+contextPath+"/dept/select?deptno="+deptno+"'>查询</a>");
                out.print("        </td>");
                out.print("    </tr>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,ps,rs);
        out.print("</table>");
        out.print("<a href='"+contextPath+"/add.html'>新增部门</a>");
        out.print("</body>");
        out.print("</html>");
    }
}
